package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000116663563";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCUiN4JBxvmS1bW523eaAEtijxUR77ZBvZhaHTXxRzhwRyqLPYSMqJFxPagkppOukvHSbOoZL42HOBoNq8Y+iz8Np8MOcp3+bLx4Gn24agIEyYUV5EYP+0QI+eNIQW8OUhhD9OAnQPckvOVvQ97zagX0i91gXi2mcrQOLf+oJNJES2L5k8ngs9CXO2HSPM0uqkXdD4pMJtK8xrQ/97zECwtx0Om/ydrInebDyLdqYNbFdfjAfTzCJQVUY3dKOgl1Sz0DD9Fjy0g0c64U7CYkuo05YMwI9GuMUuA7RpNlcaOPhSvTpd74Kqh0YodhaC+OCSeLrI+xhPEYuV17YNRGp6jAgMBAAECggEBAJPF6yiOIwql75pUmrEHRgy5l7W9vziJvbtBQeWc3FQZXNxNaO7H4zuIblloIQ6sN1HomE2zFK4fYn8r0F4IH5M5AFkYcb+U85vCSYnuDM8jxeG9EMRQbRh+sBJvRRZy4vh111UAYWQj6yUQ+AOSomY4nTDJg4AfV3B6zUsNFBs+8Kkd1k2XRq5MTqHasO25ETXaHot6+s+yrlosBP2dROoBu9JRTroR1Zri8wndUxrtnrrdffm2SgczyQD4b6Yj0TYGCku8dWBfgePvJYLChvnnlfq6QHFlw9Z2qtG38Thy9MM1ZS7QWM6R/nzh7lI5HAoRN5OOagGtB5kjtf5QyVECgYEAyJiYjXMpHNJkngqsIUkUOo2rkAqifWuZTU6X5+eZjZBn53FpgwzrYdBTUPWgLlyfwirujgEVmphs0+xSftKxiM/RBV0A7VG7ujGUUphk8vsSSBBBh/7uK8haQDL3InpLaeMh0/6kjszEtTII02zXh1zGlBiPolaApEZw3hUEFEsCgYEAvY8zQ53sxaCB1oYo/ZBY9TkcyMfYH2y0SNz5G9DyyZJPPEfASSBwr34lCqyCEgWRqSWDY6Zu4+cn97nvIAurpsXVcma0ktjrD/hX+0QRxJchxQQDlhaYIrtI5QqL2Is2j24yoMWYRM/CGxNqolb7hLMYon6gJjFW/av4O9QguAkCgYEAnY7eJBxUKoNG7KvNT8eqz+L+4oC36HNfUk1tG4XaIktOkYePLJQ1Pfr3tht32XJ/OW+nOHq5kVgwArUYhuzO7Xrt3pySYdVU8oaGzelNQVzbmybJkhw2zsTrvolJfBc6rMzugSj97/v+daP7MrXHBFdm0mQITaZMHLwHYyh2r8kCgYBPgxu7xyikFRmhRJE5Gy1VX2mFht5PACf3UElls5XDm42E+YvcoLDjzAP5oDbHHHLnAodEa3GkQzfun52AUaTkxaMs44vm9PpUrpUpHnpdfg96Np7SfBPArdJ70JqscGiobU7rRTcD1inTdaeLBPVnykARGRBQNO4KZfIwFmDLEQKBgQCAK/MJrFv2LX25asaGfiEjf6lxDJTFrKfHiXOFXMCd4eOOCIXmWxh8uoUS/U56LICn+1f38TJlwOFmbAXj7Ex0fov0feLYa//GJuRLI0vedlNw7kbfLYjjdqVWl+JGHIMIoJT7+hYku1UGK6byHySWvDRtgrWf7eSBqPuF7swVDQ==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsaGcnMuLwR0CJyW+m/Z7qpHOWSeNNPQsrpqyekYPHKoWbECYXtnOBmGDF1rlgvBYJpeYTeM6ZwLD00xIUWNJslQ2zBR04SVOPqngXSxFrsKazZTontHj7wzmAPd8MEGBsG4j2g6nnhv1ewUmGdDZUK7FiAIy9VgDprNOt5H76TkoKhUpMKKJQs5FcPwyS4AFeqnOscRskOsBaoEvyh+OvJkYqACD1IHfQpfrOplDLCiSXzLQAOpSB0dEzGNYJ8cHAgcq3eDuJbr6Qr4qtTz9B6SguHGn0kkIyFGDSZPzG+VrlMdsCv6dymAF1l4HAXbKjIE+CzSezyrTW0HU5ICkoQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url="http://txj.free.idcfengye.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                +"\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
